package com.example.ResumeWorkPlatform.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @ClassName: TimeFormate
 * @Description: 日期时间转化工具类
 * @Copyright: smartlab411
 * @Author hudaofan
 * @Date Oct 18, 2020 1:08:20 AM
 */
public class TimeFormate {

	private static final Logger log = LoggerFactory.getLogger(TimeFormate.class);

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private static SimpleDateFormat ms = new SimpleDateFormat("mm:ss");

	private static SimpleDateFormat sdfymd = new SimpleDateFormat("yyyy-MM-dd");

	private static SimpleDateFormat hm = new SimpleDateFormat("HH:mm");

	private static SimpleDateFormat mdhm = new SimpleDateFormat("MM-dd HH:mm");

	/**
	 * @param DateTime 传入Date
	 * @return 返回时间格式（2016-09-08 12:09:46）
	 */
	public static String getFormatStrYMDByDate(Date DateTime) {
		return sdf.format(DateTime);
	}

	/**
	 * @param DateTime 传入String
	 * @return 返回时间格式 Date
	 */
	public static Date getStrToDate(String DateTime) throws Exception {
		return sdf.parse(DateTime);
	}

	/**
	 * @param DateTime 传入Date
	 * @return 返回时间格式（2016-09-08）
	 */
	public static String getFormatSdfymd(Date DateTime) {
		return sdfymd.format(DateTime);
	}

	/**
	 * @param DateTime 传入Date
	 * @return 返回时间格式（09-08）
	 */
	public static String getFormatStrMDByDate(Date DateTime) {
		String format = sdf.format(DateTime);
		String time = format.substring(5, 10);
		return time;
	}

	/**
	 * @param formatStr 传入String 格式:yyyy-MM-dd HH:mm:ss
	 * @return 根据传入字符串获取 Date
	 * @throws ParseException Date
	 */
	public static Date getDateByFormatStr(String formatStr) throws ParseException {
		Date dateTime = null;
		try {
			dateTime = sdf.parse(formatStr);
		} catch (Exception e) {
			log.error("error", e);
		}
		return dateTime;
	}

	/**
	 * String->Date 传入String 格式:yyyy-MM-dd
	 * 
	 * @param formatStr
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateByFormatDateYMD(String formatStr) throws ParseException {
		Date dateTime = null;
		try {
			dateTime = sdfymd.parse(formatStr);
		} catch (Exception e) {
			log.error("error", e);
		}
		return dateTime;
	}

	/**
	 * @param millisecondDate 传入Date
	 * @return String 根据传入Date返回时间 格式: mm:ss
	 */
	public static String getStrByMillisecondDate(Date millisecondDate) {
		String format = ms.format(millisecondDate);
		return format;
	}

	/**
	 * 
	 * @Title: getStrByHMDate
	 * @Description: String 根据传入Date返回时间 格式: hh:mm
	 * @Author hudaofan
	 * @Date Oct 18, 2020 1:09:21 AM
	 * @param date
	 * @return
	 */
	public static String getStrByHMDate(Date date) {
		String format = hm.format(date);
		return format;
	}

	/**
	 * @param DateTime 传入Date
	 * @return 返回时间格式（09-08 12:09）
	 */
	public static String getFormatStrMDHMByDate(Date DateTime) {
		return mdhm.format(DateTime);
	}

	/**
	 * 
	 * @Title: getEndTime
	 * @Description: 获取当天的结束时间
	 * @Author hudaofan
	 * @Date Oct 18, 2020 1:09:40 AM
	 * @param date
	 * @return
	 */
	public static Date getEndTime(Date date) {
		Calendar dayEnd = new GregorianCalendar();
		dayEnd.setTime(date);
		dayEnd.set(Calendar.HOUR, 23);
		dayEnd.set(Calendar.MINUTE, 59);
		dayEnd.set(Calendar.SECOND, 59);
		dayEnd.set(Calendar.MILLISECOND, 0);
		return dayEnd.getTime();
	}

	/**
	 * 
	 * @Title: getStartTime
	 * @Description: 获取当天的开始时间
	 * @Author hudaofan
	 * @Date Oct 18, 2020 1:09:54 AM
	 * @param date
	 * @return
	 */
	public static Date getStartTime(Date date) {
		Calendar dayStart = new GregorianCalendar();
		dayStart.setTime(date);
		dayStart.set(Calendar.HOUR, 0);
		dayStart.set(Calendar.MINUTE, 0);
		dayStart.set(Calendar.SECOND, 0);
		dayStart.set(Calendar.MILLISECOND, 0);
		return dayStart.getTime();
	}
	/**
	 * 
	 * @Title:  isToday
	 * @Description: 判断当前是否处于当天时间范围
	 * @Author chengwenixng
	 * @Date 2021-04-08 19:39
	 * @param date
	 * @return
	 */
	public static boolean isToday(Date inputJudgeDate) {
		boolean flag = false;
		//获取当前系统时间
		long longDate = System.currentTimeMillis();
		Date nowDate = new Date(longDate);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = dateFormat.format(nowDate);
		String subDate = format.substring(0, 10);
		//定义每天的24h时间范围
		String beginTime = subDate + " 00:00:00";
		String endTime = subDate + " 23:59:59";
		Date paseBeginTime = null;
		Date paseEndTime = null;
		try {
			paseBeginTime = dateFormat.parse(beginTime);
			paseEndTime = dateFormat.parse(endTime);
			
		} catch (ParseException e) {
			log.error(e.getMessage());
		}
		if(inputJudgeDate.after(paseBeginTime) && inputJudgeDate.before(paseEndTime)) {
			flag = true;
		}
		return flag;
	}
	


}
